<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>表单管理</title>
<jsp:include page="../inc.jsp" flush="false"></jsp:include>

<script type="text/javascript"
        src="${pageContext.request.contextPath}/plug-in/custom/form.js"
        charset="utf-8"></script>
<script type="text/javascript">
Ext.onReady(function () {
    Ext.QuickTips.init();
    createGrid();
    createLayout();
    createForm();
    form = Ext.create('userForm');
});
var gridEl = 'repTime-grid';
var grid, form, selModel, toolbar, searchFlds;
var win;
function createGrid() {
    searchFlds = new Ext.util.MixedCollection();
    var fldTimeName = new Ext.form.TextField({
        name: 'tableName',
        id: gridEl + '-name',
        fieldLabel: '编码名称',
        allowBlank: true,
        width: 200
    });

    searchFlds.add('tableName', fldTimeName);
    grid = Ext.create('gridView', {
        remoteSort: true,
        gridId: gridEl,
        checkboxSelectionModel: true,
        searchFlds: searchFlds,
        entityClassName: 'com.rover.system.entity.FormTableInfo',
        queryUrl: 'formTableInfoController/query.action',
        deleteUrl: 'formTableInfoController/delete.action',
        recordPk: [ "tableId" ],
        expander: {text: '配置',
            menuDisabled: true,
            sortable: false,
            align: 'center',
            xtype: 'actioncolumn',

            items: [
                { id: 'upArraw', iconCls: 'settingsIcon', tooltip: '配置该项',
                    handler: function (grid, rowIndex, colIndex) {   //rowIndex，colIndex均从0开始
                        var rec = grid.getStore().getAt(rowIndex);
                        grid.currentRowIndex = rowIndex;
                        createValueWindows(rec.data.tableId);
                    } },
                '|',
                { id: 'generatePage', iconCls: 'settingsIcon', tooltip: '生成页面',
                    handler: function (grid, rowIndex, colIndex) {   //rowIndex，colIndex均从0开始
                        var rec = grid.getStore().getAt(rowIndex);
                        grid.currentRowIndex = rowIndex;
                        Ext.MessageBox.confirm('提示', '确定生成通用页面？',
                                function (e) {
                                    if (e == 'yes') {
                                        Ext.Ajax.request({
                                            url: getRootPath() + "/formTableInfoController/generatePage.action",
                                            method: "post",
                                            params: {tableId: rec.data.tableId},
                                            success: function () {
                                                Ext.Msg.alert("提示", "生成成功！");
                                            }
                                        });
                                    }
                                });
                    } }
            ]}
    });

}


// 刷新列表
function reloadRecords() {
    grid.getStore().reload();
}
var winValue;
function createValueWindows(tableId) {
    var url = "formTableField.jsp?tableId=" + tableId;

    winValue = new Ext.Window({
        title: '表单列',
        id: 'winValue',
        name: 'winValue',
        iconCls: 'icon-win',
        autoHeight: true,
        width: 600,
        height: 500,
        buttonAlign: 'center',
        buttons: [
            {
                text: "关闭",
                id: "btnWinClose",
                handler: function () {
                    winValue.close();
                }
            }
        ],
        layout: 'fit',
        plain: false,
        closable: false,
        modal: true,
        html: "<iframe width='100%' height='100%' frameborder='0' src='" + url + "'></iframe>"
    });

    winValue.show();
}

function createWindows() {
    if (win == null) {
        win = new Ext.Window({
            title: '新增',
            iconCls: 'icon-win',
            autoHeight: true,
            width: 335,
            height: 300,
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/formTableInfoController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    win.hide();
                                    grid.getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    },
                    id: "btnSave"
                },
                {
                    text: "关闭",
                    id: "btnClose",
                    handler: function () {
                        win.hide();
                    }
                }
            ],
            layout: 'fit',
            plain: false,
            closable: false,
            modal: true,
            items: [ form ]
        });
    }
    win.show();
}

// 创建form表单
function createForm() {
    Ext.define('userForm', {
        extend: 'Ext.form.Panel',
        fields: new Ext.util.MixedCollection(),
        initComponent: function () {
            var me = this;
            var tn = 1;

            this.fields.add("tableId", new Ext.form.Hidden({
                name: "tableId",
                dataIndex: "tableId"
            }));

            this.fields.add("tableName", new Ext.form.TextField({
                name: "tableName",
                dataIndex: "tableName",
                fieldLabel: "显示名称",
                allowBlank: false
            }));
            this.fields.add("physicalName", new Ext.form.TextField({
                name: "physicalName",
                dataIndex: "physicalName",
                fieldLabel: "数据表名",
                allowBlank: false
            }));
            this.fields.add("entityName", new Ext.form.TextField({
                name: "entityName",
                dataIndex: "entityName",
                fieldLabel: "实体名",
                allowBlank: false
            }));
            this.fields.add("entityPackage", new Ext.form.TextField({
                name: "entityPackage",
                dataIndex: "entityPackage",
                fieldLabel: "包名",
                allowBlank: false
            }));
            this.fields.add("generationType", new comboBox({
                name: "generationType",
                dataIndex: "generationType",
                fieldLabel: "主键策略",
                labelAlign: "right",
                codeName: "主键策略",
                queryMode: 'local',
                typeAhead: false,
                forceSelection: true,
                loadingText: '正在搜索...',
                listWidth: 220,
                width: 200,
                minChars: 0,
                hideTrigger: false,
                allowBlank: false,
                listeners: {
                    select: function (combo, record, index) {
                        var oracleSequence = form.getForm().findField('oracleSequence');
                        combo.value == "sequence" ? oracleSequence.allowBlank = false : oracleSequence.allowBlank = true;
                    }
                }
            }));
            this.fields.add("oracleSequence", new Ext.form.TextField({
                name: "oracleSequence",
                dataIndex: "oracleSequence",
                fieldLabel: "主键SEQUENCE:(Oracle序列名)"
            }));
            this.fields.add("isJS",  new comboBox({
                name: "isJS",
                dataIndex: "isJS",
                fieldLabel: "是否生成JS",
                labelAlign: "right",
                codeName: "是否",
                queryMode: 'local',
                typeAhead: false,
                forceSelection: true,
                loadingText: '正在搜索...',
                listWidth: 220,
                width: 200,
                minChars: 0,
                hideTrigger: false,
                allowBlank:    true
            }));
            this.items = [
                {
                    layout: "form",
                    columnWidth: 1,
                    labelAlign: "right",
                    labelWidth: 100,
                    items: [ this.fields.get("tableId"),
                        this.fields.get("tableName"), this.fields.get("physicalName") ,
                        this.fields.get("entityName"), this.fields.get("entityPackage"),
                        this.fields.get("generationType"), this.fields.get("oracleSequence") ,
                        this.fields.get("isJS")
                    ]
                }
            ];
            this.frame = false;
            this.layout = 'fit';
            this.region = 'east';
            this.border = true;
            this.split = true;
            this.callParent(arguments);
        },
        onRender: function () {
            this.callParent(arguments);
        },
        newDataRecord: function () {
            this.getForm().reset();
        }
    });
}


// 创建新的记录，初始化表单
function EditRecord() {
    var sm = grid.getSelectionModel();
    var records = sm.getSelection();
    if (records.length <= 0) {
        Ext.Msg.alert('错误', "未选中记录，不能修改！");
        return;
    }
    var data = records[0].data;
    form.getForm().loadRecord(records[0]);
    createWindows();
}
// 创建layout
function createLayout() {
    toolbar = new Ext.Toolbar({
        enableOverflow : true,
        items: [
            {
                text: "新增",
                iconCls: 'addIcon',
                id: "tlb_NEW",
                handler: function () {
                    form.newDataRecord();
                    createWindows();
                }
            },
            '-',
            {
                text: "编辑",
                iconCls: 'editIcon',
                id: "tlb_EDIT",
                handler: function () {
                    EditRecord();
                }
            },
            '-',
            {
                text: "删除",
                iconCls: 'deleteIcon',
                id: "tlb_DELETE",
                handler: function () {
                    grid.deleteRecord();
                }
            },
            "->",
            searchFlds.get("tableName"),
            {
                text: "搜索",
                iconCls: 'searchIcon',
                handler: function () {
                    grid.load();
                }
            }
        ]
    });
    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [
            {
                layout: 'fit',
                region: 'north',
                border: false,
                items: [ toolbar ]
            },
            {
                layout: 'border',
                region: 'center',
                border: false,
                items: [ grid ]
            }
        ]
    });
    viewport.doLayout();
}
</script>
</head>
<body>
</body>
</html>